package controller;

import com.hjw.entity.LoginUser;
import com.hjw.service.IUserService;
import com.hjw.service.TokenService;
import com.hjw.util.R;
import com.hjw.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class LoginController {
    @Autowired
    private IUserService userService;
    @Autowired
    private TokenService tokenService;

    @GetMapping("/login")
    public R login(@RequestBody UserVo vo){
        //1. 查询用户
        LoginUser loginUser = userService.findUser(vo);
        if(loginUser != null){
            //2. 返回到前端
            Map<String,Object> map = tokenService.createToken(loginUser);
            //3. 返回
            return R.ok(map,"登录成功！");
        }
        return R.fail("登录失败！");
    }
}
